<?php
class Language {
	private $default = 'english';
	private $parent;
	private $directory;
	private $data = array();
 	
	public function __construct($directory = null, $parent = null) {
		$this->directory = $directory;
		$this->parent = $parent;
		if ($directory == null && $parent != null) {
			$this->directory = $parent->getDirectory();
			$this->data = $this->parent->getData();
		}
	}
	
	public function getDirectory() {
		return $this->directory;
	}
	
  	public function get($key) {
  		return isset($this->data[$key]) ? $this->data[$key] : $key;
  	}
	
  	public function getData() {
  		return $this->data;
  	}
  	
	public function &all() {
		return $this->data;
	}
	
	public function load($filename, $external = false) {
		if ($external) {
			$file = $filename;
		}
		else {
			$file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
    	}
		if (file_exists($file)) {
			$_ = array();
	  		
			require($file);
		
			$this->data = array_merge($this->data, $_);
			
			return $this->data;
		}
		
		$file = DIR_LANGUAGE . $this->default . '/' . $filename . '.php';
		
		if (file_exists($file)) {
			$_ = array();
	  		
			require($file);
		
			$this->data = array_merge($this->data, $_);
			
			return $this->data;
		} else {
			//$GLOBALS['log']->write('Error: Could not load language ' . $filename . '!');
		}
  	}
}
?>